﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;

namespace KZ.Lottery
{
    public partial class FrmChangePassword : Form
    {
        public FrmChangePassword()
        {
            InitializeComponent();
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtOldPassword.Text)) { throw new BusinessLogicException(KZ_Message.BLM_ERR_OLDPASSWORD_REQUIRED); }
                if (String.IsNullOrEmpty(txtNewPassword.Text)) { throw new BusinessLogicException(KZ_Message.BLM_ERR_NEWPASSWORD_REQUIRED); }
                if (txtConfirmPassword.Text != txtNewPassword.Text) { throw new BusinessLogicException(KZ_Message.BLM_ERR_CONFIRMPASSWORD_INCORRECT); }

                Setting setting = SettingBLL.GetSetting(KZ_Resource.Setting_Password);
                if (txtOldPassword.Text != setting.SettingValue) { throw new BusinessLogicException(KZ_Message.BLM_ERR_PASSWORD_INCORRECT); }

                List<Setting> settingList = new List<Setting>();
                settingList.Add(new Setting { SettingID = KZ_Resource.Setting_Password, SettingValue = txtNewPassword.Text });

                if (SettingBLL.UpdateSetting(settingList))
                {
                    Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_CHANGEPASSWORD, true);
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("btnChangePassword_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }
    }
}